package com.sg.xy.controller;

import com.sg.xy.pojo.Token;
import com.sg.xy.pojo.UserInfo;
import com.sg.xy.service.CodeService;
import com.sg.xy.service.TokenService;
import com.sg.xy.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {
    @Reference
    private CodeService codeService;

    @Autowired
    private UserService userService;

    @Autowired
    private TokenService tokenService;

    /**
     * 注册接⼝，true成功，false失败
     *
     * @return
     */
    @GetMapping("/register/{email}/{password}/{code}")
    public String register(@PathVariable("email") String email, @PathVariable("password") String password, @PathVariable("code") String code, HttpServletResponse response) {
        if (isRegistered(email)) {
            // 已经注册过
            return "-1";
        }
        String forObject = codeService.validate(email, code);
        if("1".equals(forObject) || "2".equals(forObject)) {
            return forObject;
        }
        userService.save(email, password);
        return 0 + "";
    }

    /**
     * 是否已注册，根据邮箱判断,true代表已经注册过，
     * false代表尚未注册
     *
     * @param email
     * @return
     */
    @GetMapping("/isRegistered/{email}")
    public Boolean isRegistered(@PathVariable String email) {
        UserInfo userInfo = userService.getByEmail(email);
        return userInfo != null;
    }

    /**
     * 登录接⼝，验证⽤户名密码合法性，根据⽤户名和
     * 密码⽣成token，token存⼊数据库，并写⼊cookie
     * 中，登录成功返回邮箱地址，重定向到欢迎⻚
     *
     * @return 邮箱地址
     */
    @GetMapping("/login/{email}/{password}")
    public String login(@PathVariable String email, @PathVariable String password, HttpServletResponse response) {
        UserInfo userInfo = userService.getByEmail(email);
        if (!Objects.equals(password, userInfo.getPassword())) {
            return "false";
        }
        String s = UUID.randomUUID().toString();
        Cookie cookie = new Cookie("token", s);

        response.addCookie(cookie);
        Token token = new Token();
        token.setEmail(email);
        token.setToken(s);
        Cookie emailCookie = new Cookie("email",email);
        emailCookie.setPath("/");
        response.addCookie(emailCookie);
        tokenService.save(token);
        return "true";
    }

    /**
     * 根据token查询⽤户登录邮箱接⼝
     *
     * @param token
     * @return
     */
    @GetMapping("/info/{token}")
    public Object info(@PathVariable String token) {
        Token t = tokenService.getByToken(token);
        return t.getEmail();
    }
}
